<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2017-7-21
 * Func   注册
 */
defined('IN_YuLin') || exit('NO PERMIT!');
$seo['title'] = '大众彩票-注册账户';
$pageTitle    = '注册';
$navTag       = 'member';
$page         = 'member/register';

if (IS_POST){
    $operation = NoBadStr($_POST['operation']);
    
    switch ($operation){
        case 'reg':
            $username	= NoBadStr($_POST['username']);
            $password	= NoBadStr($_POST['password']);
            $password2	= NoBadStr($_POST['repassword']);
            $verify		= NoBadStr($_POST['imgcode']);
            $invitaCode = NoBadStr($_POST['invitation']);
            include_once LIB_PATH."class/Secoder.class.php";
            if(!Secoder::check($verify)){
                $error = '验证码错误';
                break;
            }
            
            if(strlen($username) < 2 || strlen($username) > 16){
                $error = '账号为2-16位';
                break;
            }
            if(strlen($password) < 2 || strlen($password) > 20){
                $error = '密码为2-20位';
                break;
            }
            if($password != $password2){
                $error = '确认密码不正确';
                break;
            }
            $user = $db->getrow('SELECT 1 FROM '.Table('user').' WHERE username = \''.$username.'\'');
            if ($user){
                $error = '该账号已经被注册';
                break;
            }
            // 检查邀请码是否使用过
            $codeInfo  = $db->getrow('SELECT id, uid, remark, status, type, is_once FROM ' . Table('user_invita_code') . ' WHERE invita_code = "' . $invitaCode . '"');
            if (!$codeInfo) {
                $error = '邀请码不正确';
                break;
            }
            if ($codeInfo['is_once'] == '2'){
                // 如果是一次性的，检查是否有使用过
                $checkIsUse     = $db->getrow('SELECT id FROM ' . Table('user_info') . ' WHERE invita_code_id = ' . $codeInfo['id']);
                if ($checkIsUse['id']) {
                    $error = '单次邀请码，已使用过';
                    break;
                }
            }
            if ($codeInfo['status'] == '2'){
                if ($checkIsUse['id']) {
                    $error = '邀请码无效';
                    break;
                } 
            }
            
            //	插入数据库
            $ins = [];
            $ins['username']	= $username;
            $ins['password']	= Password($password);
            $ins['reg_time']	= TIMESTAMP;
            $ins['reg_ip']		= $_Y['ip'];
            $ins['last_time']	= TIMESTAMP;
            $ins['last_ip']		= $_Y['ip'];
            $ins['status']		= 1;
            $ins['platform']    = 1;
            $ins['head_pic']    = 'static/images/head_pic/1_(' . rand(1, 102) . ').jpg';
            $id                 = $db->add('user',$ins);
            if (!$id){
                $error = '注册失败，请重新注册';
                break;
            }
            $ins2 = [];
            $ins2['uid']	   = $id;
            $ins2['truename']  = $realname;
            $ins2['mobile']	   = $phone;
            $ins2['qq']		   = $qq;
            $ins2['invita_code_id'] = $codeInfo['id'];
            $ins2['user_type']      = $codeInfo['type'];
            $db->add('user_info',$ins2);
            //	推荐人
            $upuid             = intval($_GET['ruid']);
            !$upuid && $upuid  = intval($_SESSION['upuid']);
            if($upuid)
            {
                $tmp = $db->getrow('SELECT 1 FROM '.Table('user').' WHERE id = '.$upuid);
                !$tmp && $upuid = 0;
            }
            $tmp             = [];
            $tmp['uid']		 =	$id;
            $tmp['upuid']	 =	$upuid;
            $db->add('user_status',$tmp);
            $upuid && $db->exec('UPDATE '.Table('user_status').' SET sons = sons + 1 WHERE uid = '.$upuid);
            $_SESSION['uid'] = $id;
            set_cookie('LoginSessionID',session_id());
            
            // 当前注册一个用户， 对每个上面所有关系级别的用户的下级用户累加
            $userLineData = array();
            getUserLineUp($id, $userLineData);
            $fidStr = implode(',', array_keys($userLineData));
            $db->exec('UPDATE ' . Table('user_info') . ' SET lower_nums = lower_nums + 1 WHERE uid IN (' . $fidStr . ')');
            $redirect        = U('member/index');
            header('Location:' . $redirect);
            break;
    }
}

$tpl->display($page);